//
//  LeftViewController.m
//  check
//
//  Created by 大连网医科技 on 14/12/1.
//  Copyright (c) 2014年 大连网医科技. All rights reserved.
//

#import "LeftViewController.h"
#import "YRSideViewController.h"
#import "CustomView.h"
#import "HelpViewController.h"
#import "SettingsViewController.h"
#import "PersonalCenterViewController.h"
#import "MyDynamicViewController.h"
#import "GlobalMacro.h"
#import "YRSideViewController.h"
#import "URLMacro.h"
#import "AFNetWorkingYU.h"
#import "GlobalMethod.h"
#import "UIImageView+WebCache.h"
#import "LeftTableViewCell.h"
static LeftViewController *shareViewController;

@interface LeftViewController ()

@end

@implementation LeftViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LINEA" object:nil];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.labelArray = [NSMutableArray array];
        self.tViewArray = [NSMutableArray array];
        [_tViewArray addObject:@"首页"];
        [_tViewArray addObject:@"我的动态"];
        [_tViewArray addObject:@"个人中心"];
        [_tViewArray addObject:@"帮助"];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lineAnimation:) name:@"LINEA" object:nil];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self startConnect];
}

- (void)loadView
{
    [super loadView];
    self.customView = [[CustomView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/3*2, SCREEN_HEIGHT)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 按钮添加点击事件
    self.customView.tableView.delegate = self;
    self.customView.tableView.dataSource = self;
    [self.customView.settingButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_customView];
    
    // 抽屉效果里的头像和名字点击事件
    [_customView.headerButton addTarget:self action:@selector(headerImageAction:) forControlEvents:UIControlEventTouchUpInside];
    [_customView.nameButton addTarget:self action:@selector(headerImageAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)startConnect
{
    NSString *approveKey = [GlobalMethod GetIOSUUID];
    NSString *parame = [GlobalMethod getApproveKey];
    NSString *url = [NSString stringWithFormat:@"%@?approveKey=%@&deviceParam=%@", URL_USER_GETDETAIL, approveKey, parame];
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:url resultBlock:^(id result) {
        if ([result isKindOfClass:[NSDictionary class]]) {
        self.dic = [NSDictionary dictionaryWithDictionary:result];
        // 头像
        id objDic = [_dic objectForKey:@"obj"];
        if ([objDic isKindOfClass:[NSDictionary class]]) {
            NSString *imgUrl = [NSString stringWithFormat:@"http://www.isu.so/attached/u_img/%@", [[_dic objectForKey:@"obj"] objectForKey:@"img"]];
            [_customView.headerImg sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
                
            [_customView.headerButton addTarget:self action:@selector(headerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                
                // 姓名
            [_customView.nameLabel setText:[[_dic objectForKey:@"obj"] objectForKey:@"nick"]];
            [_customView.nameButton addTarget:self action:@selector(headerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                
                // 学校
            NSNull *null = [[NSNull alloc] init];
            if ([[_dic objectForKey:@"obj"] objectForKey:@"school"] == null) {
                    [_customView.schoolLabel setText:@""];
                    [_customView.instituteLabel setText:@""];
                } else {
                    NSArray *schoolArr = [[[_dic objectForKey:@"obj"] objectForKey:@"school"] componentsSeparatedByString:@"大学"];
                    NSString *school = [NSString stringWithFormat:@"%@大学", [schoolArr firstObject]];
                    if (![[schoolArr firstObject] isEqualToString:[schoolArr lastObject]]) {
                        [_customView.schoolLabel setText:school];
                        [_customView.instituteLabel setText:[schoolArr lastObject]];
                    }else {
                        [_customView.instituteLabel setText:[schoolArr firstObject]];
                    }
                }
            }
        } else {
#warning 
        }
    }];
}


- (void)headerButtonAction:(UIButton *)sender
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"我得动态" forKey:@"text"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSH" object:self userInfo:dic];
}

- (void)buttonAction:(UIButton *)sender
{

    NSDictionary *dic = [NSDictionary dictionaryWithObject:sender.titleLabel.text forKey:@"text"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSH" object:self userInfo:dic];
    
}

//单例模式创建侧边栏
+ (LeftViewController *)share
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareViewController = [[LeftViewController alloc] init];
    });
    return shareViewController;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tViewArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"buttonCell"];
    cell.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
    NSString *str = [_tViewArray objectAtIndex:indexPath.row];
    [cell.selectLabel setText:str];
    // 将4个label放到一个数组里
    [_labelArray addObject:cell.selectLabel];
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0;
}
//
- (void)backRootViewController
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"首页" forKey:@"text"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSH" object:self userInfo:dic];
    
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    
    [self.customView.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取出selectLabel(此label和cell里对应的label指向的内存地址相同)
    UILabel *selectLabel = [_labelArray objectAtIndex:indexPath.row];
    // 遍历数组, 设置label的字体颜色
    for (UILabel *label in _labelArray) {
        label.textColor = TEXTERGRAY;
        // 设置被选中的label字体颜色为绿色
        if ([label.text isEqualToString:selectLabel.text]) {
            label.textColor = TEXTGREENCOLOR;
        }
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *str = [_tViewArray objectAtIndex:indexPath.row];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:str forKey:@"text"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSH" object:self userInfo:dic];
}

- (void)lineAnimation:(NSNotification *)notify
{
    NSString *move = [notify.userInfo objectForKey:@"move"];
    if ([move isEqualToString:@"right"]) {
        [UIView animateWithDuration:0.3 animations:^{
            _customView.lineA.frame = CGRectMake(SCREEN_WIDTH, 0, 0.5, SCREEN_HEIGHT);
        }];
    } else if ([move isEqualToString:@"left"]) {
        [UIView animateWithDuration:0.3 animations:^{
            _customView.lineA.frame = CGRectMake(SCREEN_WIDTH / 3 * 2 - 0.5, 0, 0.5, SCREEN_HEIGHT);
        }];
    }
}

#pragma mark - 头像点击事件, 点击跳转到个人中心
- (void)headerImageAction:(UIButton *)button
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"个人中心" forKey:@"text"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSH" object:self userInfo:dic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
